package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.combat.Option;
import com.itheima.mapper.OptionMapper;
import com.itheima.service.db.OptionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

//选项
@Service
public class OptionServiceImpl implements OptionService {
    @Autowired
    private OptionMapper optionMapper;

    //根据试题的id查询所有的选项
    @Override
    public List<Option> findOptionById(Integer questionSoulId) {
        List<Option> list = optionMapper.findOptionById(questionSoulId);
        return list;
    }

    //根据试题的ID和选项的Id查询出分数
    @Override
    public Option findScore(String questionId, String optionId) {
        Option option = optionMapper.findOptionByQidOrOId(questionId, optionId);
        return option;
    }

    //保存选项
    @Override
    public void saveOption(Option option) {
        //保存选项
        optionMapper.saveOption(option);
    }

    //根据问题的Id删除选项
    @Override
    public void deleteQuestion(String questionId) {
        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_soul_id", questionId);
        optionMapper.delete(queryWrapper);
    }

    //修改题目
    @Override
    public void updateOption(List<String> optionContextList,String questionId) {
        int i = 0;
        List<Integer> optionIdList = new ArrayList<>();
        List<Option> optionById = optionMapper.findOptionById(Integer.parseInt(questionId));
        if (CollectionUtil.isNotEmpty(optionById)) {
            i = optionById.size();
            for (Option option : optionById) {
                optionIdList.add(option.getId());
            }
        }

        if (CollectionUtil.isNotEmpty(optionContextList)) {
            for (String s : optionContextList) {
                optionMapper.updateByOptionId(optionIdList.get(i++),s);
            }
        }
    }
}
